// adapted from as1107

/**************************************************************************/
/*! 
    @file     as1107.c
    @author   K. Townsend (microBuilder.eu)
	
    @brief    Drivers for the AS1107 I2C LED/segment-display driver

    @section LICENSE

    Software License Agreement (BSD License)

    Copyright (c) 2012, microBuilder SARL
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holders nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#include "../driver/as1107.h"
#include "../core/systick.h"
#include "../core/softspi.h"

//extern volatile uint8_t   I2CMasterBuffer[I2C_BUFSIZE];
//extern volatile uint8_t   I2CSlaveBuffer[I2C_BUFSIZE];
//extern volatile uint32_t  I2CReadLength, I2CWriteLength;

static bool _as1107Initialised = false;
//
///**************************************************************************/
///*!
//    @brief  Sends a single command byte over I2C to the specified address
//*/
///**************************************************************************/
//as1107Error_t as1107WriteCmd (uint8_t cmd)
//{
//  // Clear write buffers
////  uint32_t i;
////  for ( i = 0; i < I2C_BUFSIZE; i++ )
////  {
////    I2CMasterBuffer[i] = 0x00;
////  }
////
////  // ToDo: Add real I2C error checking
////  I2CWriteLength = 2;
////  I2CReadLength = 0;
////  I2CMasterBuffer[0] = address;               // device sub address
////  I2CMasterBuffer[1] = cmd;                   // Command register
////  i2cEngine();
//
//	//write_spi(cmd);
//
//
//  return AS1107_ERROR_OK;
//}

/**************************************************************************/
/*! 
    @brief  Writes an command byte followed by a data byte over I2C
*/
/**************************************************************************/
as1107Error_t as1107WriteCmdData (uint8_t cmd, uint8_t data)
{
	as1107write8(cmd,data);
	return AS1107_ERROR_OK;
}
//
///**************************************************************************/
///*!
//    @brief  Reads a single byte over I2C
//*/
///**************************************************************************/
//as1107Error_t as1107Read8(uint8_t address, uint8_t *value)
//{
//  // Clear write buffers
////  uint32_t i;
////  for ( i = 0; i < I2C_BUFSIZE; i++ )
////  {
////    I2CMasterBuffer[i] = 0x00;
////  }
////
////  // ToDo: Add real I2C error checking
////  I2CWriteLength = 0;
////  I2CReadLength = 1;
////  I2CMasterBuffer[0] = address | AS1107_READBIT;
////  i2cEngine();
////
////  *value = I2CSlaveBuffer[0];
//
//  return AS1107_ERROR_OK;
//}

/**************************************************************************/
/*! 
    @brief  Initialises the I2C block
*/
/**************************************************************************/
as1107Error_t as1107Init(void)
{
//  // Initialise I2C
	// disable test mode
    as1107WriteCmdData(AS1107_DISPTEST, 0x00);

  // Reset AS1107 to normal operation with default settings
	as1107WriteCmdData(AS1107_SHUTDOWN, 0x01);

//  systickDelay(20);

  // Use hex decoding
	as1107WriteCmdData(AS1107_DECODEMODE, 0xff);

  // Set the brightness to the maximum value (0x0F)
  as1107SetBrightness(0xF);

  // Turn on all digits by default (0..7)
  as1107WriteCmdData(AS1107_SCANLIMIT, 0x07);


  _as1107Initialised = true;

  return AS1107_ERROR_OK;
}

/**************************************************************************/
/*! 
    @brief  Send test command
*/
/**************************************************************************/
as1107Error_t as1107Test(void)
{
  as1107Error_t error = AS1107_ERROR_OK;

  if (!_as1107Initialised) as1107Init();

  // Turn everything on
  error = as1107WriteCmdData(AS1107_DISPTEST, 0x01);
  if (error) return error;  
  //systickDelay(10);

  // Turn everything off
  error = as1107WriteCmdData(AS1107_DISPTEST, 0x00);
  if (error) return error;
  
  return error;
}


/**************************************************************************/
/*! 
    @brief  Sets the intensity control register (0..15 or 0x00..0x0F)
*/
/**************************************************************************/
as1107Error_t as1107SetBrightness(uint8_t x)
{
  as1107Error_t error = AS1107_ERROR_OK;

  if (x > 0xF) x = 0xF;

  error = as1107WriteCmdData(AS1107_INTENSITY, x);
  return error;
}

/**************************************************************************/
/*! 
    @brief  Sets the feature bit (controls blinking, clock source, etc.)
*/
/**************************************************************************/
as1107Error_t as1107SetFeature(uint8_t feature)
{
  as1107Error_t error = AS1107_ERROR_OK;

  error = as1107WriteCmdData(AS1107_FEATURE, feature);
  return error;
}

/**************************************************************************/
/*! 
    @brief  Writes an 8-bit buffer to the display
*/
/**************************************************************************/
as1107Error_t as1107WriteBuffer(uint8_t *buffer)
{
  uint32_t i;

  if (!_as1107Initialised) as1107Init();

  // Update individual digits
  for ( i = 0; i < 8; i++ )
  {
	  if(MY_DISPLAY)
	  as1107write8(i+1,buffer[i]);

  }
  
  return AS1107_ERROR_OK;
}  
